﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class Employees_EmpLeave : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent();

            Master.DataSource = WMaster;
            if (Request["action"].ToString() != "add")
            {
                WMaster.SetWhere("EmpLeaveId=" + Request["lid"].ToString());

                hidEid.Value = Request["eid"].ToString();
                hidEname.Value = Request["ename"].ToString();
            }


       
            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "员工离职";
            master.PicUrl = "";
            master.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Employees_EmpLeave));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DataCompressed = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.EmpLeave";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }


    protected void wnHr_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdFirst" || e.CommandName == "cmdPrevious"
            || e.CommandName == "cmdNext" || e.CommandName == "cmdLast")
        {
            Master.ExecuteSelect(EmpLeave);
            DataBind();
        }
        else if (e.CommandName == "cmdAdd")
        {
            //Master.ExecuteAdd(Contract);
            DataBind();

        }
        else if (e.CommandName == "cmdDelete")
        {
            Master.ExecuteSelect(EmpLeave);
            DataBind();
        }
        else if (e.CommandName == "cmdApply" && EmpLeave.AllValidateSucess)
        {
            Master.ExecuteSelect(EmpLeave);
            DataBind();
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('执行成功');location.href='" + Request.Url.ToString() + "'",true);
        }
    }

    protected void EmpLeave_DataBound(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            if (Request["action"].ToString() == "edit")
            {
                wn.NavControls.Remove(wn.NavControls["Add"]);

                DataRowView drv = (DataRowView)EmpLeave.DataItem;
                string LeaveReason = drv["LeaveReason"].ToString();


                Control_SelUser su = EmpLeave.FindControl("SelUser") as Control_SelUser;
                su.ReadOnly(hidEid.Value, hidEname.Value);

                GetCheckListBind(Convert.ToInt32(drv["LeaveReason"]), EmpLeave.FindControl("LeaveReason") as CheckBoxList);

            }
            else
            {
                wn.NavControls.Remove(wn.NavControls["Update"]);

                if (hidEid.Value != "")
                {
                    Control_SelUser su = EmpLeave.FindControl("SelUser") as Control_SelUser;
                    su.ReadOnly(hidEid.Value, hidEname.Value);

                    GetCheckListBind(Convert.ToInt32(hidReason.Value), EmpLeave.FindControl("LeaveReason") as CheckBoxList);
                }
                else
                {
                    Control_SelUser su = EmpLeave.FindControl("SelUser") as Control_SelUser;
                    su.SelUserInti("", "");
                }
            }

        }

    }

    protected void Master_Adding(object sender, Srvtools.WebDataSourceAddingEventArgs e)
    {
        try
        {
            Control_SelUser su = EmpLeave.FindControl("SelUser") as Control_SelUser;

            if (su.EmployeeId == "")
            {
                return;
            }
            else
            {

                e.AddingValues["EmployeeId"] = su.EmployeeId;
                e.AddingValues["EditEmpId"] = Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]);
                CheckBoxList cbl = EmpLeave.FindControl("LeaveReason") as CheckBoxList;

                int Reason = 0;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected)
                    {
                        Reason += Convert.ToInt32(cbl.Items[i].Value);
                    }
                }
                if (Reason != 0)
                {
                    e.AddingValues["LeaveReason"] = Reason;
                    hidReason.Value = Reason.ToString();
                }

                hidEid.Value = su.EmployeeId;
                hidEname.Value = su.EmpName;


            }
        }
        catch
        {
            return;
        }
    }

    private void GetCheckListBind(int Reason, CheckBoxList cbl)
    {
        int Rid = Reason;
        int RoundInt = 1;
       

        while (RoundInt > 0)
        {
            if (RoundInt * 2 < Rid)
            {
                RoundInt = RoundInt * 2;
            }
            else if (Rid <= 2 || RoundInt * 2 == Rid)
            {

                
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Value == Rid.ToString())
                    {
                        cbl.Items[i].Selected = true;
                        break;
                    }
                }

                RoundInt = -1;
            }

            else
            {
                
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Value == RoundInt.ToString())
                    {
                        cbl.Items[i].Selected = true;
                        break;
                    }
                }

                Rid = Rid - RoundInt;
                RoundInt = 1;
            }
        }



    }

    protected void Master_Updating(object sender, Srvtools.WebDataSourceUpdatingEventArgs e)
    {
        e.UpdatingValues["EditEmpId"] = Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]);
        CheckBoxList cbl = EmpLeave.FindControl("LeaveReason") as CheckBoxList;

        int Reason = 0;
        for (int i = 0; i < cbl.Items.Count; i++)
        {
            if (cbl.Items[i].Selected)
            {
                Reason += Convert.ToInt32(cbl.Items[i].Value);
            }
        }
        if (Reason != 0)
        {
            e.UpdatingValues["LeaveReason"] = Reason;
        }
    }
}
